<?php
include_once 'model/banner/process.php';
include_once 'module/controls.php';
include_once 'model/user/user_create.php';
$c = new controls();
$banner = new banner();
$user = new users();
$fillByName = $user->FillOneByName($_SESSION);
$result = $banner->FillAllByTop();
$rsRight = $banner->FillAllByRight();
$level_decode = json_decode($fillByName["level"], true);
?>
<div id="content">
    <p>
        <span class="bglinkleft"><a class="link" href="<?= $url["home"] ?>/menu/banner-manager"><?php echo $phrase["banner_top"] ?></a></span>
        <?php if ($level_decode["admin"] != "" || $level_decode["create"] != "") { ?>
            <span class="bglinkleft"><a class="link" href="<?= $url["home"] ?>/menu/banner-create"><?php echo $phrase["banner_create"] ?></a></span>
        <?php } ?>
    </p>
    <form method="post" action="<?= $url["home"] ?>/menu/banner-process">
        <p>
        <ul id="sortable">
            <?php
            while ($rw = mysql_fetch_array($rsRight)) {
                ?>
                <li class="ui-state-default-cate"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>
                    <?php
                    $value = array("banner_id" => $rw["banner_id"], "order" => $rw["banner_order"]);
                    ?>
                    <input type="hidden" value='<?php print_r(json_encode($value)) ?>' name="banner_value[]" id="banner_value[]"/>
                    <span class="right">
                        <?php if ($level_decode["admin"] != "" || $level_decode["update"] != "") { ?>
                            <a href="<?= $url["home"] ?>/menu/cateupdate/tag/0/id/<?php echo $rw["banner_id"] ?>">
                                <image src="<?= $url["home"] ?>/images/icons/edit-icon.gif"/>
                            </a>
                        <?php } ?>
                        <?php if ($level_decode["admin"] != "" || $level_decode["update"] != "" && $level_decode["delete"]) { ?>
                            | 
                        <?php } ?>
                        <?php if ($level_decode["admin"] != "" || $level_decode["delete"] != "") { ?>
                           <a href="<?= $url["home"] ?>/menu/pagedelete/id/<?php echo $rows["banner_id"] ?>" onclick="return confirm('Bạn có chắc chắn muốn xóa không.');">
                                <image src="<?= $url["home"] ?>/images/icons/b_drop.png"/>
                            </a>
                        <?php } ?>
                    </span>
                    <span class="center">
                        <image src="<?php echo $rw["banner_url"] ?>" width="50" height="50"/>
                    </span>
                </li>
            <?php } ?>
            <?php if (mysql_num_rows($result) > 0) { ?>
                <?php echo $c->get_button("updatebanner", "update") ?>
            <?php } ?>
        </ul>
        </p>
    </form>
</div>
<script>
    $(function() {
        $( "#sortable" ).sortable({
            start: function(event, ui) {
                var start_pos = ui.item.index();
                ui.item.data('start_pos', start_pos);
            },
            change: function(event, ui) {
                var start_pos = ui.item.data('start_pos');
                var index = ui.placeholder.index();
                if (start_pos < index) {
                    $('#sortable li:nth-child(' + index + ')').addClass('highlights');
                } else {
                    $('#sortable li:eq(' + (index + 1) + ')').addClass('highlights');
                }
            },
            update: function(event, ui) {
                //                   var start_pos = ui.item.data('start_pos');
                //                   var end_pos = ui.item.index();
                $("li.ui-state-default-cate").each(function(index){
                    console.log(index);
                    var oS = $.parseJSON($('li.ui-state-default-cate').eq(index).children('input').val());
                    console.log(oS);
                    var arrS = '{"banner_id":"'+oS.banner_id+'","order":"'+index+'"}';
                    $('li.ui-state-default-cate').eq(index).children('input').val(arrS);
                });
            }
        });
        $("#sortable").disableSelection(); 
    });
</script>